<template>
  <div class="wxconfig-edit">
    <el-dialog
      v-bind="$attrs"
      :title="state.dialogTitle"
      v-model="state.modalVisible"
      :before-close="cancelDialog"
      :append-to-body="true"
      :destroy-on-close="true"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :lock-scroll="false"
      class="wxconfig-dialog"
      draggable
    >
      <!-- content -->
      <el-form ref="wxconfigRef" :model="state.form" :rules="state.rules" label-position="top">
        <el-row justify="center" :gutter="60" style="width: 100%; margin: 0 !important; padding: 0 !important; margin: 0 auto">
          <el-col :span="24">
            <el-tabs type="border-card" v-model="state.tabsKey">
              <el-tab-pane label="微信公众号配置" name="微信公众号配置">
                <el-row :gutter="60">
                  <!-- <el-col :span="12" v-if="Object.keys(state.editView).length">
                    <el-form-item label="logo上传" prop="logonImage">
                      <UploadFile
                        v-model:fileID="state.form.logonImage"
                        :attId="state.form.wxpz_aimodelid"
                        multiple
                        accept="image/*"
                        :limit="2"
                        attmodel="aiimage"
                        ref="sfzUploadRef"
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12" v-if="Object.keys(state.editView).length"> </el-col> -->
                  <el-col :span="12">
                    <el-form-item label="微信公众号名称" prop="wxpz_wxname">
                      <el-input v-model="state.form.wxpz_wxname" placeholder="公众号名称" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="是否启用" prop="wxpz_status">
                      <el-switch v-model="state.form.wxpz_status" :active-value="1" :inactive-value="0"></el-switch>
                      <!-- <el-input v-model="state.form.wxpz_status" placeholder="是否启用" /> -->
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="公众号id" prop="wxpz_appid">
                      <el-input v-model="state.form.wxpz_appid" placeholder="公众号id" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="微信code" prop="wxpz_wxcode">
                      <el-input v-model="state.form.wxpz_wxcode" placeholder="微信code" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="微信appsecret" prop="wxpz_appsecret">
                      <el-input v-model="state.form.wxpz_appsecret" placeholder="微信appsecret" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="微信接口token" prop="wxpz_token">
                      <el-input v-model="state.form.wxpz_token" placeholder="微信接口token" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="微信消息密钥" prop="wxpz_encodingaeskey">
                      <el-input v-model="state.form.wxpz_encodingaeskey" placeholder="微信消息密钥" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="关联模型id" prop="wxpz_aimodelid">
                      <el-input v-model="state.form.wxpz_aimodelid" placeholder="关联模型id" />
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-tab-pane>
              <el-tab-pane label="微信关注欢迎信息" name="微信关注欢迎信息">
                <el-row :gutter="60">
                  <el-col :span="24">
                    <el-form-item label="微信关注欢迎信息" prop="wxpz_gzwelcomemsg">
                      <!-- <Editor
                        :modelValue="state.form.wxpz_gzwelcomemsg"
                        :minHeight="200"
                        @update:modelValue="
                          (text) => {
                            state.form.wxpz_gzwelcomemsg = text
                          }
                        "
                      ></Editor> -->
                      <el-input v-model="state.form.wxpz_gzwelcomemsg" style="width: 100%" :rows="10" type="textarea" placeholder="输入微信关注欢迎信息" />
                      <!-- <el-input style="display: none" type="textarea" :rows="7" v-model="state.form.wxpz_gzwelcomemsg" placeholder="微信关注欢迎信息" /> -->
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-tab-pane>
              <el-tab-pane label="H5欢迎信息" name="H5欢迎信息">
                <el-row :gutter="60">
                  <el-col :span="24">
                    <el-form-item label="H5欢迎信息" prop="">
                      <Editor
                        :modelValue="state.h5Config.h5welcomemessage"
                        :minHeight="200"
                        @update:modelValue="
                          (text) => {
                            state.h5Config.h5welcomemessage = text
                          }
                        "
                      ></Editor>
                      <!-- <el-input v-model="state.form.wxpz_gzwelcomemsg" style="width: 100%" :rows="10" type="textarea" placeholder="Please input" /> -->
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-tab-pane>
              <el-tab-pane label="验证短信内容" name="验证短信内容">
                <el-row :gutter="60">
                  <el-col :span="12">
                    <el-form-item label="微信发送小于最小长度字符提示内容" prop="wxpz_sendmintxtlenmsg">
                      <el-input type="textarea" :rows="3" v-model="state.form.wxpz_sendmintxtlenmsg" placeholder="微信发送小于最小长度字符提示内容" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="内容为空时的提示信息" prop="wxpz_textisnullmsg">
                      <el-input type="textarea" :rows="3" v-model="state.form.wxpz_textisnullmsg" placeholder="内容为空时的提示信息" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="验证短信内容" prop="wxpz_vifsmsmsg">
                      <Editor
                        :modelValue="state.form.wxpz_vifsmsmsg"
                        :minHeight="200"
                        @update:modelValue="
                          (text) => {
                            state.form.wxpz_vifsmsmsg = text
                          }
                        "
                      ></Editor>
                      <el-input style="display: none" type="textarea" :rows="6" v-model="state.form.wxpz_vifsmsmsg" placeholder="验证短信内容" />
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-tab-pane>
              <el-tab-pane label="短信验证文本" name="短信验证文本">
                <el-row :gutter="60">
                  <el-col :span="12">
                    <el-form-item label="验证短信发送成功信息" prop="wxpz_vifsendsuccessmsg" class="decoration-slate-50">
                      <template #label>
                        验证短信
                        <span class="text-green-500">发送成功</span>
                        信息
                      </template>
                      <el-input type="textarea" :rows="3" v-model="state.form.wxpz_vifsendsuccessmsg" placeholder="验证短信发送成功信息" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="验证短信发送失败信息" prop="wxpz_vifsendfailmsg">
                      <template #label>
                        验证短信
                        <span class="text-red-500">发送失败</span>
                        信息
                      </template>
                      <el-input type="textarea" :rows="3" v-model="state.form.wxpz_vifsendfailmsg" placeholder="验证短信发送失败信息" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="验证短信超出次数信息" prop="wxpz_vifsmsexceedCountmsg">
                      <template #label>
                        验证短信
                        <span class="text-orange-500">超出次数</span>
                        信息
                      </template>
                      <el-input type="textarea" :rows="3" v-model="state.form.wxpz_vifsmsexceedCountmsg" placeholder="验证短信超出次数信息" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="验证短信信息" prop="wxpz_vifsmsovermsg">
                      <template #label>
                        验证短信
                        <span class="text-green-500">验证成功</span>
                        信息
                      </template>
                      <el-input type="textarea" :rows="3" v-model="state.form.wxpz_vifsmsovermsg" placeholder="验证短信验证成功信息" />
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="发送文本达多少次触发验证" prop="wxpz_sendtxtvifCount">
                      <el-input v-model.number="state.form.wxpz_sendtxtvifCount" placeholder="发送文本达多少次触发验证">
                        <template #append>次数</template>
                      </el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="验证短信发送超过多少次停止发送" prop="wxpz_vifsmsCount">
                      <el-input v-model.number="state.form.wxpz_vifsmsCount" placeholder="验证短信发送超过多少次停止发送">
                        <template #append>次数</template>
                      </el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="微信发送最小长度字符限制" prop="wxpz_sendmintxtlen">
                      <el-input v-model="state.form.wxpz_sendmintxtlen" placeholder="微信发送最小长度字符限制">
                        <template #append>字符</template>
                      </el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-tab-pane>
            </el-tabs>
          </el-col>
          <!-- <el-divider content-position="left" :class="'!mb-8'">微信公众号配置</el-divider>
          <el-divider content-position="left" :class="'!mb-8'">短信验证配置</el-divider> -->
        </el-row>
      </el-form>

      <!-- footer -->
      <template #footer>
        <div class="dialog-footer">
          <!-- h5配置 -->
          <el-button v-if="state.tabsKey == 'H5欢迎信息'" type="primary" @click="submitH5Dialog">确 定</el-button>
          <!-- 微信配置 -->
          <el-button v-else type="primary" @click="submitDialog">确 定</el-button>
          <el-button @click="cancelDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import useWXconfigManageManageStore from '@store/modules/wxconfigManage'
import useAImodelManageStore from '@store/modules/aimodelManage'
import {filterObject} from '@/utils/index'
import {nextTick} from 'vue'

const wxconfigManageStore = useWXconfigManageManageStore()
const {proxy} = getCurrentInstance()
const wxconfigRef = ref(null)
const aimodelManageStore = useAImodelManageStore()

const state = reactive({
  modalVisible: false,
  dialogTitle: '',
  editView: {},
  tabsKey: '微信公众号配置',
  form: {
    wxpz_wxname: '',
    wxpz_wxcode: '',
    wxpz_appid: '',
    wxpz_appsecret: '',
    wxpz_token: '',
    wxpz_encodingaeskey: '',
    wxpz_status: '',
    wxpz_aimodelid: '',
    wxpz_vifsmsmsg:
      '✅ 您好，我是基于最新人工智能ChatGPT大模型开发的 $botname，很高兴与您交流。如需进一步交流的话，我需要对您进行手机验证，请输入一个真实有效的手机号，我会给你发送一个验证码，输入验证码即可。',
    wxpz_sendtxtvifCount: '',
    wxpz_vifsendfailmsg: '您好，短信发送失败，请检查手机号码是否正确！',
    wxpz_vifsendsuccessmsg: '我已经给 $mobilenum 发送了验证码，请注意查收',
    wxpz_vifsmsCount: '5',
    wxpz_vifsmsexceedCountmsg: '超出验证次数，请联系管理员',
    wxpz_vifsmsovermsg: '验证成功，感谢您的配合！请继续提问吧',
    wxpz_gzwelcomemsg:
      '✅ 您好！我是飞驰AI客服，这是一款基于最新人工智能ChatGPT大模型框架开发，由Feature365. LLC (USA) 研发的人工智能机器人，专注于帮助客户解答 智能客服 和 市场营销 相关话题的定制机器人，支持100多种语言无缝切换（包括但不限于英语、中文、西班牙语、法语、德语、俄语、日语和韩语），自动学习并提高专业技能，全天侯智能对话，可以帮助企业解决各种客服问题，提升服务质量和效率。本产品在中国区域由四川省飞驰未来科技有限公司运营。########您可以直接输入文字与我交流，我会尽力帮助您解答《飞驰AI客服》的相关问题，同时我们也可以一起探讨有关人工智能和市场营销相关话题。',
    wxpz_sendmintxtlen: '1',
    wxpz_sendmintxtlenmsg:
      '✅ 您好，我是基于最新人工智能ChatGPT大模型开发的人工智能机器人，很高兴与您交流。请至少输入2个汉字，你提出的问题我都能理解，因为我是世界上最聪明的机器人',
    wxpz_textisnullmsg: '目前暂时只能处理文本信息',
    logonImage: ''
  },
  h5Config: {
    h5welcomemessage: ''
  },
  rules: {
    // account: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
    // username: [{ required: true, message: '姓名/昵称不能为空', trigger: 'blur' }],
    // userworkgroup1: [{ required: true, message: '关联公司不能为空', trigger: 'blur' }],
    // userworkgroup2: [{ required: true, message: '关联部门不能为空', trigger: 'blur' }],
  }
})

const emits = defineEmits(['refresh'])

const getDataView = (wxpz_id) => {
  nextTick(async () => {
    const result = await wxconfigManageStore.view({'wxpz_id.Value': wxpz_id})
    state.editView = result
    state.form = filterObject(result, Object.keys(state.form))
  })
}

const upDataEditor = (text) => {
  state.form.wxpz_vifsmsmsg = text
}

const submitH5Dialog = async () => {
  await aimodelManageStore.up(state.h5Config)
  proxy.$modal.msgSuccess('修改成功！')
}

const submitDialog = () => {
  wxconfigRef.value.validate(async (valid) => {
    if (valid) {
      if (Object.keys(state.editView).length) {
        state.form.wxpz_id = state.editView.wxpz_id
        await wxconfigManageStore.up(state.form)
        proxy.$modal.msgSuccess('修改成功！')
      } else {
        await wxconfigManageStore.add(state.form)
        proxy.$modal.msgSuccess('新增成功！')
      }
      state.editView = {}
      wxconfigRef.value.resetFields()
      emits('refresh')
      state.modalVisible = false
    }
  })
}

const cancelDialog = () => {
  state.editView = {}
  wxconfigRef.value.resetFields()
  state.modalVisible = false
}

const getH5View = (aiid) => {
  nextTick(async () => {
    const result = await aimodelManageStore.view({aiid: aiid})
    state.h5Config.aiid = aiid
    state.h5Config = filterObject(result, Object.keys(state.h5Config))
  })
}

const openDialog = async (data = {}) => {
  console.log(data.wxpz_aimodelid, 'wxpz_aimodelid')
  if (Object.keys(data).length) await getDataView(data.wxpz_id)
  state.modalVisible = true
  state.dialogTitle = Object.keys(data).length ? `编辑【${data.wxpz_wxname}】` : '新增'
  if (data.wxpz_aimodelid) {
    // 如果关联模型id 获取h5欢迎信息
    await getH5View(data.wxpz_aimodelid)
  }
}

defineExpose({
  openDialog
})
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>
